package com.ktwlrj.securitycenter.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ktwlrj.core.base.ActionResult;
import com.ktwlrj.core.base.SuperController;
import com.ktwlrj.core.exception.ActionException;
import com.ktwlrj.core.util.ContextHolderUtils;
import com.ktwlrj.core.validation.group.Insert;
import com.ktwlrj.core.validation.group.Update;
import com.ktwlrj.securitycenter.controller.args.*;
import com.ktwlrj.securitycenter.dto.SafeApproveInterceptDto;
import com.ktwlrj.securitycenter.dto.SafeInterceptDto;
import com.ktwlrj.securitycenter.dto.SafeInterceptPageDto;
import com.ktwlrj.securitycenter.mapper.SafeApproveMapper;
import com.ktwlrj.securitycenter.service.SafeInterceptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

/**
 * 预警拦截控制层
 *
 * @author code generation
 * @date 2020-02-24 18:08:13
 * @since JDK1.8
 */
@Slf4j
@Validated
@RestController
@RequiredArgsConstructor
@Api(tags = "SafeIntercept", value = "SafeInterceptApi", description = "预警拦截接口相关")
@RequestMapping(value = "safeIntercept", produces = MediaType.APPLICATION_JSON_VALUE)
public class SafeInterceptController extends SuperController {
    private final SafeInterceptService service;
    private final SafeApproveMapper approveMapper;
    private final ContextHolderUtils contextHolderUtils;


    @ApiOperation(value = "添加(v1.0.0)", notes = "添加")
    @PostMapping(value = "/insert")
    public ActionResult<String> insert(@RequestBody @Validated(Insert.class) SafeInterceptArgs args) throws ActionException {
        args.setInterceptType(1);
        service.save(args);
        return success("添加成功");
    }


    @ApiOperation(value = "通过主键id修改(v1.0.0)", notes = "修改")
    @PostMapping(value = "/update")
    public ActionResult<Object> update(@RequestBody @Validated(Update.class) SafeInterceptArgs args) throws ActionException {
        service.updateById(args);
        return success("修改成功");
    }


    @ApiOperation(value = "变更添加(v1.0.0)", notes = "变更添加")
    @PostMapping(value = "/changeInsert")
    public ActionResult<String> changeInsert(@RequestBody @Validated(Update.class) SafeInterceptArgs args) throws ActionException {
        args.setInterceptType(2);
        service.changeInsert(args);
        return success("变更提交成功");
    }


    @ApiOperation(value = "提交(v1.0.0)", notes = "提交")
    @PostMapping(value = "/submit")
    public ActionResult<String> submit(@RequestBody @Valid SafeInterceptApplyArgs args) throws ActionException {
        service.submit(args);
        return success("提交成功");
    }


    @ApiOperation(value = "逻辑删除(v1.0.0)", notes = "删除")
    @ApiImplicitParam(paramType = "query", value = "主键id", name = "interceptId", required = true)
    @GetMapping(value = "/delete")
    public ActionResult<String> delete(@NotBlank(message = "主键id不能为空") String interceptId) throws ActionException {
        service.deletedById(interceptId);
        return success("删除成功");
    }


    @ApiOperation(value = "删除申请(v1.0.0)", notes = "删除申请")
    @PostMapping(value = "/deleteApply")
    public ActionResult<String> deleteApply(@RequestBody @Valid SafeInterceptApplyArgs args) throws ActionException {
        service.deleteApply(args);
        return success("删除申请提交成功");
    }


    @ApiOperation(value = "通过主键id查询详情(v1.0.0)", notes = "查询详情")
    @ApiImplicitParam(paramType = "query", value = "主键id", name = "interceptId", required = true)
    @GetMapping(value = "/select/by-id")
    public ActionResult<SafeInterceptDto> getById(@NotBlank(message = "主键id不能为空") String interceptId) throws ActionException {
        return success(service.getById(interceptId));
    }


    @ApiOperation(value = "分页查询(v1.0.0)", notes = "分页查询")
    @PostMapping(value = "/select/page")
    public ActionResult<IPage<SafeInterceptPageDto>> selectPage(@RequestBody @Valid SafeInterceptPageArgs args) throws ActionException {
        return success(service.page(args));
    }


    @ApiOperation(value = "历史记录分页查询(v1.0.0)", notes = "历史记录分页查询")
    @PostMapping(value = "/select/historyPage")
    public ActionResult<IPage<SafeInterceptPageDto>> selectHistoryPage(@RequestBody @Valid SafeInterceptPageArgs args) throws ActionException {
        args.setApproveStatus(40);
        return success(service.page(args));
    }


    @ApiOperation(value = "预警拦截审批分页查询(v1.0.0)", notes = "预警拦截审批分页查询")
    @PostMapping(value = "/select/approvePage")
    public ActionResult<IPage<SafeApproveInterceptDto>> selectApprovePage(@RequestBody @Valid SafeApprovePageArgs args) throws ActionException {
        args.setHandlePersonId(contextHolderUtils.getUserId());
        return success(approveMapper.interceptPage(args.getQueryPage(), args));
    }


    @ApiOperation(value = "预警拦截审批(v1.0.0)", notes = "预警拦截审批")
    @PostMapping(value = "/approveSubmit")
    public ActionResult<Object> approveSubmit(@RequestBody @Valid SafeApproveArgs args) throws ActionException {
        service.approveSubmit(args);
        return success("审批成功");
    }
}